/*ball.js*/
var scene = $('div.scene');
var winW = $(window).width();
var winH = $(window).height();

//总共参与抽奖的人数
var totalperson = 214;

scene.css({
	width: winW + 'px',
	height: winH + 'px'
});

/*-----------------------------初始化抽奖数据---------------------------------------*/

var initdatas = localStorage.getItem('initdatas') ? JSON.parse(localStorage.getItem('initdatas')) : [];
if (!initdatas.length) {
	for (var i = 0; i < totalperson; i++) {
		if (i < 10) {
			initdatas.push('00' + i);
		} else if (i < 100) {
			initdatas.push('0' + i);
		} else {
			initdatas.push(i + '');
		};
	};
	localStorage.setItem('initdatas', JSON.stringify(initdatas))
};


/*-----------------------------------开始抽奖-----------------------------------------*/
var one = $('#one');
var two = $('#two');
var three = $('#three');
var ball = $('#ball');
var out = $('div.out');
var selectnumber = $('div.select-number');
var totalClass = [];
var allClass = [];
var firstType = "";

//已中奖的号码
var isWinningeds = localStorage.getItem('isWinningeds') ? JSON.parse(localStorage.getItem('isWinningeds')) : [];
var isWinningedsJson = localStorage.getItem('isWinningedsJson') ? JSON.parse(localStorage.getItem('isWinningedsJson')) : [];

// 根据参数获得随机数
function getNumberForRadom(max, min) {
	return parseInt(Math.random() * ((max - min) + 1));
};

//获得中奖号码
function winningFn(type) {
	console.log(JSON.stringify(isWinningeds.sort(function(x, y) {
		return x - y
	})));
	var oneth = getNumberForRadom(9, 0); //个位
	var twoth = getNumberForRadom(9, 0); //十位

	var threeth = parseInt(oneth + twoth * 10); //十位
	var baith = totalperson.toString().substring(1, 2); //切取后面两位数

	var isrepeat = false; //是否中过奖

	// 中奖的索引号
	var winningeIndex = 0;

	//中奖号码索引
	if (threeth > baith) {
		winningeIndex = getNumberForRadom(1, 0) * 100 + threeth
	} else {
		winningeIndex = getNumberForRadom(2, 0) * 100 + threeth
	};

	// 判断是否是重复中奖号码
	if (isWinningeds && isWinningeds.length) {
		for (var i = 0; i < isWinningeds.length; i++) {
			if (isWinningeds[i] === winningeIndex) {
				isrepeat = true;
				console.log(winningeIndex + '------------------------');
			}
		};
	};

	if (isrepeat) {
		return winningFn(type);
	} else {
		// 保存已经中奖的号码
		isWinningeds.push(winningeIndex);
		localStorage.setItem('isWinningeds', JSON.stringify(isWinningeds))
		isWinningedsJson.push({
			type: type,
			value: initdatas[winningeIndex]
		});
		localStorage.setItem('isWinningedsJson', JSON.stringify(isWinningedsJson))
		return winningeIndex
	};
};

// 中奖翻牌HTML
function showHaveHTML(type) {
	// 判断是否换了抽奖等级
	if (firstType && firstType != type) {
		scene.find('div.select-number').remove();
	};

	var selectNumbers = scene.find('div.select-number');
	if (!selectNumbers.length) {

		showJiangText(type); //显示标题

		var html = ""
		var newArr = [];
		if (isWinningedsJson && isWinningedsJson.length) {
			for (var i = 0; i < isWinningedsJson.length; i++) {
				if (isWinningedsJson[i].type == type) {
					newArr.push(isWinningedsJson[i])
				};
			};
		};
		if (newArr && newArr.length) {
			for (var i = 0; i < newArr.length; i++) {
				html += '<div class="select-number list" style="right:-145px;top:' + (15 + (i * 40)) + 'px;transform: scale(0.28,0.28);margin:0;">\
						<div class="block">\
							<div class="row bai totoprow">\
								<span>0</span>\
								<span>' + newArr[i].value.toString()[0] + '</span>\
							</div>\
							<div class="row shi totoprow">\
								<span>0</span>\
								<span>' + newArr[i].value.toString()[1] + '</span>\
							</div>\
							<div class="row ge totoprow">\
								<span>0</span>\
								<span>' + newArr[i].value.toString()[2] + '</span>\
							</div>\
						</div>\
					</div>';
			};
		};
		// scene.append(html);
		$('.select-numbox').append(html);
	};
};


// 开始抽奖
function getJiangPing(type) {

	//显示已经中奖的号码
	console.log(JSON.stringify(isWinningedsJson));
	showHaveHTML(type);

	// 禁止连续点击
	$('button.btn').attr('disabled', true);

	// 是否显示获奖标题
	showJiangText(type);
	firstType = type;

	/*------------------------------抽奖逻辑开始------------------------------------*/

	// 中奖号码
	var winninge = initdatas[winningFn(type)];

	// 塞入中奖html
	var zhongjiangclass = randomString(6);

	// 界面上显示中奖号码
	getHTML(winninge, zhongjiangclass);

	// 打印中奖号码和索引
	console.log('中奖的索引' + winninge);

	/*------------------------------抽奖逻辑结束------------------------------------*/

	// 存入数组
	allClass.push({
		classname: zhongjiangclass,
		type: type,
	});
	totalClass.push({
		classname: zhongjiangclass,
		type: type,
	});

	// 生成抽奖右移动画
	var keyframes = "@keyframes " + zhongjiangclass + "{\
				0% {opacity:1;right:50%;top:50%;transform: scale(1,1);margin:-100px -240px 0 0;}\
				50%{opacity:0.1;}\
				100% {opacity:1;right:-145px;top:" + (15 + ((scene.find('div.select-number').length - 1) * 40)) + "px;transform: scale(0.28,0.28);margin:0;}\
			}";
	$('#styles').append(keyframes);

	if (type == 4 || type == 5) {

		$('#ball').css({
			animation: 'choujiang 0.8s cubic-bezier(0.025, 0.735, 0.025, 0.990) 1 forwards',
		});
		out.css({
			animation: 'choujiangScale2 1s 0.8s forwards',
		});

		$('#ball').one("animationend", function() {
			$('div.' + zhongjiangclass).css({
				animation: 'selectnumberanimate 1s forwards',
			});
		});
		// 翻牌
		$('div.' + zhongjiangclass).one("animationend", function() {
			$($('div.' + zhongjiangclass).find('.row')[0]).addClass('totoprow');
			setTimeout(function() {
				$($('div.' + zhongjiangclass).find('.row')[1]).addClass('totoprow');
			}, 1000);
			setTimeout(function() {
				$($('div.' + zhongjiangclass).find('.row')[2]).addClass('totoprow');

				// 球放大并回复运转
				setTimeout(function() {
					out.css({
						animation: 'choujiangScale3 2s forwards',
					});

					// 球恢复运行
					$('#ball').css({
						animation: 'myball 50s linear infinite',
					});
				}, 1000);

				// 移动奖牌
				$('div.' + zhongjiangclass).one("animationend", function() {
					$('div.' + zhongjiangclass).css({
						animation: zhongjiangclass + ' 2s forwards',
					});

					setTimeout(function() {
						$('div.' + zhongjiangclass).addClass('list');
					}, 1000);

					// 运动完成允许继续抽奖
					$('div.' + zhongjiangclass).one("animationend", function() {
						$('button.btn').removeAttr("disabled");
					});
				});

				// 清除动画
				// out.css({
				// 	animation: 'none',
				// });

				// 是否显示获奖标题
				showJiangText(type);
			}, 2800);
		});
	}else {

		$('#ball').css({
			animation: 'choujiang 2s cubic-bezier(0.025, 0.735, 0.025, 0.990) 1 forwards',
		});
		out.css({
			animation: 'choujiangScale2 2s 2s forwards',
		});

		$('#ball').one("animationend", function() {
			$('div.' + zhongjiangclass).css({
				animation: 'selectnumberanimate 1s forwards',
			});
		});
		// 翻牌
		$('div.' + zhongjiangclass).one("animationend", function() {
			$($('div.' + zhongjiangclass).find('.row')[0]).addClass('totoprow');
			setTimeout(function() {
				$($('div.' + zhongjiangclass).find('.row')[1]).addClass('totoprow');
			}, 1500);
			setTimeout(function() {
				$($('div.' + zhongjiangclass).find('.row')[2]).addClass('totoprow');

				// 球放大并回复运转
				setTimeout(function() {
					out.css({
						animation: 'choujiangScale3 2s forwards',
					});

					// 球恢复运行
					$('#ball').css({
						animation: 'myball 50s linear infinite',
					});
				}, 1000);

				// 移动奖牌
				$('div.' + zhongjiangclass).one("animationend", function() {
					$('div.' + zhongjiangclass).css({
						animation: zhongjiangclass + ' 2s forwards',
					});

					setTimeout(function() {
						$('div.' + zhongjiangclass).addClass('list');
					}, 1000);

					// 运动完成允许继续抽奖
					$('div.' + zhongjiangclass).one("animationend", function() {
						$('button.btn').removeAttr("disabled");
					});
				});

				// 清除动画
				// out.css({
				// 	animation: 'none',
				// });

				// 是否显示获奖标题
				showJiangText(type);
			}, 3000);
		});
	}
	
};

// 显示获奖标题
function showJiangText(type) {
	if (scene.find('div.select-number').length) {
		switch (type) {
			case 1:
				$('#jiangpingsspan').text('一等奖');
				break;
			case 2:
				$('#jiangpingsspan').text('二等奖');
				break;
			case 3:
				$('#jiangpingsspan').text('三等奖');
				break;
			case 4:
				$('#jiangpingsspan').text('幸运奖');
				break;
			case 5:
				$('#jiangpingsspan').text('RP奖');
				break;
		};
		$('div.jiangpingtext').addClass('show').removeClass('hide');
	} else {
		$('div.jiangpingtext').addClass('hide').removeClass('show');
	};
};

// 中奖翻牌HTML
function getHTML(number, classname) {
	var html = '<div class="select-number ' + classname + '">\
			<div class="block">\
				<div class="row bai">\
					<span>0</span>\
					<span>' + number.toString()[0] + '</span>\
				</div>\
				<div class="row shi">\
					<span>0</span>\
					<span>' + number.toString()[1] + '</span>\
				</div>\
				<div class="row ge">\
					<span>0</span>\
					<span>' + number.toString()[2] + '</span>\
				</div>\
			</div>\
		</div>';
	// scene.append(html);
	$('.select-numbox').append(html);
};

/*生成随机字符串*/
function randomString(len) {　　
	len = len || 32;　　
	var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ 　　
	var maxPos = $chars.length;　　
	var pwd = '';　　
	for (i = 0; i < len; i++) {　　　　
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));　　
	}　　
	return pwd;
}